﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using NHibernate;
namespace Com.CMS263.DAO
{
    public class GuestDAO:BaseService
    {
        
        public Guest Add(Guest guest)
        {
            DBHelper.TransSaveOrUpdate(guest);
            return guest;
        }
        public void Delete(GuestDAO guest) 
        {
            DBHelper.TransDelete(guest);
        }
        public void Delete(int id)
        {
            DBHelper.TransDeleteByID("Guest", id);   
        }

        public bool isExist(string userName)
        {
            ISession session = DBHelper.GetISession;
            return session.CreateQuery("from Guest as g  where g.userName='" + userName + "'").List().Count > 0;
        }
        public bool isExistByU(string UserId)
        {
            ISession session = DBHelper.GetISession;
            return session.CreateQuery("from Guest as g  where g.UserId='" + UserId + "'").List().Count > 0;
        }

        public Guest Login(string userEmail, string password)
        {
            IList<Guest> list = DBHelper.ExecuteHQL("from Guest where UserEmail='" + userEmail + "' and UserPass='" + password + "' and State=1").List<Guest>();
            if (list.Count > 0)
            {
                return list.First();
            }
            else
            {
                return null;
            }
        }
        public Guest LoginByVip(string userVip, string password)
        {
            IList<Guest> list = DBHelper.ExecuteHQL("from Guest where userId='" + userVip + "' and userPass='" + password + "'  and State=1").List<Guest>();
            if (list.Count > 0)
            {
                return list.First();
            }
            else
            {
                return null;
            }
        }

        public IList<Guest> GetGuestList(int PageSize,int pageIndex)
        {

            return DBHelper.ExecuteHQL("from Guest order by id desc ").SetFirstResult((pageIndex-1)*PageSize).SetMaxResults(PageSize).List<Guest>();
        }
        public int GetTotalNum()
        {
            return Convert.ToInt32(DBHelper.ExecuteHQL("select count(*) from Guest").UniqueResult());
        }
        public bool UpdateGuest(Guest guest)
        {
            try
            {
                DBHelper.TransSaveOrUpdate(guest);
                return true;
            }
            catch 
            {
                return false;
            }
        }
        public Guest GetGuestById(int id)
        {
            try
            {
                return DBHelper.ExecuteHQL("from Guest where Id =" + id).List<Guest>().First(); ;
            }
            catch 
            {
                return null;
            }
        }
        /// <summary>
        /// 根据编号查找用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Guest GetGuestByUserId(string userId)
        {
            try
            {
                return DBHelper.ExecuteHQL("from Guest where userId ='" + userId+"'").List<Guest>().First(); ;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 检测用户邮箱是否唯一的
        /// </summary>
        /// <param name="userEmail"></param>
        /// <returns></returns>
        public bool GetLogEmail(string userEmail)
        {
            try
            {
                return DBHelper.ExecuteHQL("from Guest where UserEmail='" + userEmail + "'").List<Guest>().Count >= 1;
            }
            catch { return false; }
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="guest"></param>
        /// <returns></returns>
        public bool InsertReg(Guest guest)
        {
            try
            {
                DBHelper.TransInsert(guest);
                return true;
            }
            catch { return false; }
        }
        /// <summary>
        /// 用户管理
        /// </summary>
        /// <returns></returns>
        public IList<Guest> GetAllUser()
        {
            try { return DBHelper.ExecuteHQL("from Guest order by AddDate desc").List<Guest>(); }
            catch { return null; }
        }
        public IList<Guest> GetGuestList(string str, int PageSize, int pageIndex)
        {
            try
            {
                string hql = "from Guest";
                if (str != null && str != "")
                {
                    hql += " where (UserEmail like '%" + str + "%' or UserName like '%"+str+"%')";
                }
                    return DBHelper.ExecuteHQL(hql+"order by AddDate desc").SetFirstResult((pageIndex - 1) * PageSize).SetMaxResults(PageSize).List<Guest>();
            }
            catch { return null; }
        }
        public int GetCount(string str)
        {
            try
            {
                string hql = "from Guest";
                if (str != null && str != "")
                {
                    hql += " where (UserEmail like '%" + str + "%' or UserName like '%" + str + "%')";
                }
                return DBHelper.ExecuteHQL(hql + "order by AddDate desc").List<Guest>().Count;
            }
            catch { return 0; }
        }
        public bool Edit(Guest guest)
        {
            try {
                DBHelper.TransSaveOrUpdate(guest);
                return true;
            }
            catch { return false; }
        }
        public Guest GetUserIdByUserEmail(string userEmail)
        {
            return DBHelper.ExecuteHQL("from Guest where UserEmail='" + userEmail + "'").List<Guest>().First();
        }

        /// <summary>
        /// 根据email查询实体
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public Guest FindByEmail(string email)
        {
            try
            {
                String hql = "from Guest Where UserEmail='" + email + "'";
                return FindList<Guest>(hql, null, null, null)[0];
                 
            }
            catch { return null; }
        }
    }

}
